package com.ruoyi.goodsInfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.goodsInfo.domain.GoodsBrand;
import com.ruoyi.goodsInfo.mapper.GoodsBrandMapper;
import com.ruoyi.goodsInfo.service.GoodsBrandService;
import com.ruoyi.goodsInfo.vo.GoodsAddVo;
import com.ruoyi.goodsInfo.vo.GoodsVo;
import com.ruoyi.utils.LongUUIDGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.goodsInfo.mapper.GoodsMapper;
import com.ruoyi.goodsInfo.domain.Goods;
import com.ruoyi.goodsInfo.service.GoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * goodsInfoService业务层处理
 *
 * @author ruoyi
 * @date 2024-01-08
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsBrandMapper goodsBrandMapper;

    @Autowired
    private GoodsBrandService goodsBrandService;

    /**
     * 查询goodsInfo
     *
     * @param id goodsInfo主键
     * @return goodsInfo
     */
    @Override
    public GoodsVo selectGoodsById(Long id) {
        GoodsVo goodsVo = new GoodsVo();
        Goods goods = goodsMapper.selectGoodsById(id);
        BeanUtils.copyProperties(goods, goodsVo);
        Long brandId = null;
        List<GoodsBrand> brands = goodsBrandMapper.getBrand(id);
        if (brands.size() > 0) {
            brandId = brands.get(0).getBrandId();
            goodsVo.setBrandId(brandId);
        }
        return goodsVo;
    }

    /**
     * 查询goodsInfo列表
     *
     * @param goods goodsInfo
     * @return goodsInfo
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods) {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增goodsInfo
     *
     * @param goods goodsInfo
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGoods(GoodsAddVo goodsVo) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVo, goods);
        Long brandId = goodsVo.getBrandId();
        goods.setCreateTime(DateUtils.getNowDate());
//        goods.setId(LongUUIDGeneratorUtil.generateLongUUID());
        this.save(goods);
        //手机id与品牌绑定
        insertBrand(goods, brandId);


        return 1;
    }

    private void insertBrand(Goods goods, Long brandId) {
        GoodsBrand goodsBrand = new GoodsBrand();
        goodsBrand.setGoodsId(goods.getId());
        goodsBrand.setBrandId(brandId);
        boolean save = goodsBrandService.save(goodsBrand);
        if (!save) {
            throw new ServiceException("新增失败");
        }
    }

    /**
     * 修改goodsInfo
     *
     * @param goods goodsInfo
     * @return 结果
     */
    @Override
    @Transactional
    public int updateGoods(GoodsAddVo goodsAddVo) {
        //更新商品信息
        updateGoodsInfo(goodsAddVo);
        //更新商品分类表信息
        updateGoodsBrand(goodsAddVo);
        return 1;
    }

    private void updateGoodsBrand(GoodsAddVo goodsAddVo) {
        //根据商品id查到对于那条记录
        Long goodsId = goodsAddVo.getId();
        List<GoodsBrand> goodsBrands = goodsBrandMapper.getBrand(goodsId);
        GoodsBrand goodsBrand = new GoodsBrand();
        if (goodsBrands.size() > 0) {
            goodsBrand = goodsBrandService.getById(goodsBrands.get(0).getId());
        }
        goodsBrand.setBrandId(goodsAddVo.getBrandId());
        boolean save = goodsBrandService.updateById(goodsBrand);
        if (!save) {
            throw new ServiceException("新增失败");
        }
    }

    private void updateGoodsInfo(GoodsAddVo goodsAddVo) {
        Long goodsId = goodsAddVo.getId();
        Goods goods = this.getById(goodsId);
        if (goods == null) {
            throw new ServiceException("查无此商品");
        }
        BeanUtils.copyProperties(goodsAddVo, goods);
        goods.setUpdateTime(DateUtils.getNowDate());
        this.updateById(goods);
    }

    /**
     * 批量删除goodsInfo
     *
     * @param ids 需要删除的goodsInfo主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteGoodsByIds(Long[] ids) {
        for (Long id : ids) {
            List<GoodsBrand> brands = goodsBrandMapper.getBrand(id);
            for (GoodsBrand brand : brands) {
                goodsBrandService.removeById(brand.getId());
            }
        }
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除goodsInfo信息
     *
     * @param id goodsInfo主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteGoodsById(Long id) {
        List<GoodsBrand> brands = goodsBrandMapper.getBrand(id);
        for (GoodsBrand brand : brands) {
            goodsBrandService.removeById(brand.getId());
        }
        return goodsMapper.deleteGoodsById(id);
    }

    @Override
    public List<GoodsVo> searchGoodsList(String goodsName, String priceLevel) {


        List<GoodsVo> goodsVos = goodsMapper.searchList(goodsName, priceLevel);


        return goodsVos;
    }
}
